package cn.rkylin.oms.base.resendReasonType.service;

import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.aftersale.resend.dao.IAfterSaleResendDAO;
import cn.rkylin.oms.aftersale.resend.vo.ResendVO;
import cn.rkylin.oms.base.resendReasonType.dao.IResendReasonTypeDAO;
import cn.rkylin.oms.base.resendReasonType.domain.ResendReason;
import cn.rkylin.oms.base.resendReasonType.vo.ResendReasonVO;
import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.LogUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.jexl.context.HashMapContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018-3-21.
 */
@Service
public class ResendReasonTypeService extends ApolloService {

    @Autowired
    private LogUtil logUtil;

    @Autowired
    private IResendReasonTypeDAO resendReasonTypeDAO;

    @Autowired
    public IAfterSaleResendDAO afterSaleResendDAO;
    /**
     * 查询退款原因类型
     *
     * @param page 页号
     * @param rows 页大小
     * @param resendReasonVO 退款原因类型VO
     */
    public PageInfo<ResendReasonVO> findByWhere(int page, int rows, ResendReasonVO resendReasonVO) throws Exception {
        PageInfo<ResendReasonVO> resendReasonVOList = findPage(page, rows, "pageSelectResendReasonType", resendReasonVO);
        return resendReasonVOList;
    }

    public Map<String, Object> deleteResendReasonById(String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isBlank(id)){
            map.put("result", "failed");
            map.put("msg", "没有要删除的数据");
            return map;
        }
        ResendReasonVO resendReasonVO = resendReasonTypeDAO.findById(id);
        if (isUsed(resendReasonVO.getResendReasonCode())){
            map.put("result", "failed");
            map.put("msg", "正在使用中不能修改，删除");
            return map;
        }

        ResendReasonVO vo = new ResendReasonVO();
        vo.setResendReasonId(id);
        vo.setDeleted("y");
        vo.setUpdateTime(new Date());
        resendReasonTypeDAO.update(vo);
        logUtil.info(new DBLog(vo.getResendReasonId(), vo.getDeleted(), "删除补发原因类型 ", vo.toString()));
        map.put("result", "success");
        map.put("msg", "");
        return map;
    }

    public Map<String, Object> updateResendReason(ResendReasonVO resendReason) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (resendReason==null){
            map.put("result", "failed");
            map.put("msg", "没有要生成的数据");
            return map;
        }

        ResendReasonVO resendReasonVO = resendReasonTypeDAO.findById(resendReason.getResendReasonId());
        if (resendReasonVO==null){
            map.put("result", "failed");
            map.put("msg", "要修改的数据不存在");
            return map;
        }

        if (!resendReasonVO.getResendReasonName().equals(resendReason.getResendReasonName()) && sameName(resendReason.getResendReasonName())){
            map.put("result", "failed");
            map.put("msg", "存在同样名字的数据");
            return map;
        }
        if (!resendReasonVO.getResendReasonName().equals(resendReason.getResendReasonName()) && isUsed(resendReason.getResendReasonCode())){
            map.put("result", "failed");
            map.put("msg", "正在使用中不能修改，删除");
            return map;
        }
        resendReason.setUpdateTime(new Date());
        resendReasonTypeDAO.update(resendReason);

        logUtil.info(new DBLog(resendReason.getResendReasonId(), resendReason.getResendReasonName(), "修改补发原因类型 ", resendReason.toString()));
        map.put("result", "success");
        map.put("msg", "");
        return map;
    }

    public Map<String, Object> inserResendReason(ResendReasonVO resendReason) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (resendReason==null){
            map.put("result", "failed");
            map.put("msg", "没有要生成的数据");
            return map;
        }

        if (StringUtils.isBlank(resendReason.getResendReasonCode())){
            map.put("result", "failed");
            map.put("msg", "原因编码没有输入");
            return map;
        }

        if (StringUtils.isBlank(resendReason.getResendReasonName())){
            map.put("result", "failed");
            map.put("msg", "原因没有输入");
            return map;
        }

        if (StringUtils.isBlank(resendReason.getEntId())){
            map.put("result", "failed");
            map.put("msg", "没有企业ID");
            return map;
        }

        if (resendReasonTypeDAO.findByCode(resendReason.getResendReasonCode())!=null){
            map.put("result", "failed");
            map.put("msg", "编码："+resendReason.getResendReasonCode()+"的数据已经存在");
            return map;
        }

        if (resendReasonTypeDAO.findByName(resendReason.getResendReasonName())!=null){
            map.put("result", "failed");
            map.put("msg", "名字："+resendReason.getResendReasonName()+"的数据已经存在");
            return map;
        }

        String id = java.util.UUID.randomUUID().toString().replace("-", "");
        resendReason.setResendReasonId(id);
        resendReason.setCreateTime(new Date());
        resendReason.setUpdateTime(new Date());
        resendReasonTypeDAO.insert(resendReason);

        logUtil.info(new DBLog(id, resendReason.getResendReasonName(), "创建补发原因类型 ", resendReason.toString()));
        map.put("result", "success");
        map.put("msg", "");
        return map;
    }

    public ResendReasonVO getResendReasonById(String id)throws Exception{
        if (StringUtils.isBlank(id)){
            return null;
        }
        return resendReasonTypeDAO.findById(id);
    }

    private boolean sameName(String name){
        boolean flg = false;

        if (StringUtils.isBlank(name)){
            return false;
        }
        ResendReasonVO vo = new ResendReasonVO();
        vo.setResendReasonName(name);
        try {
            ResendReasonVO nameVO = resendReasonTypeDAO.findByName(name);
            if (nameVO!=null){
                flg=true;
            } else {
                flg=false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flg;
    }

    private boolean isUsed(String code){
        boolean flg = false;

        if (StringUtils.isBlank(code)){
            return false;
        }
        ResendVO vo = new ResendVO();
        vo.setResendReasonId(code);
        try {
            ResendVO countVO = afterSaleResendDAO.getCount(vo);
            if (countVO.getCount()>0){
                flg=true;
            } else {
                flg=false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flg;
    }
}
